import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: () => '/manager/home',},
    { path: '/manager/express', redirect: () => '/manager/express/allPackage',},
    { path: '/manager/info', redirect: () => '/manager/info/admin',},
    { path: '/manager', meta: { title: '快递易达' } , redirect: () => '/manager/home', component: () => import('@/views/Manager.vue'),
        children: [
          { path: 'home', meta: { title: '首页'}, component: () => import('@/views/Home.vue'),},
          { path: 'express/allPackage', meta: { title: '全部包裹', parent: '快递管理'}, component: () => import('@/views/Package.vue'), props: { packageState: '' },}, // 空字符串表示全部状态
          { path: 'express/unshipped', meta: { title: '待发货', parent: '快递管理'}, component: () => import('@/views/Package.vue'), props: { packageState: '待发货' },},
          { path: 'express/shipped', meta: { title: '已发货', parent: '快递管理'}, component: () => import('@/views/Package.vue'), props: { packageState: '已发货' },},
          { path: 'express/unsigned', meta: { title: '待签收', parent: '快递管理'}, component: () => import('@/views/Package.vue'), props: { packageState: '待签收' },},
          { path: 'express/signed', meta: { title: '已签收', parent: '快递管理'}, component: () => import('@/views/Package.vue'), props: { packageState: '已签收' },}, // 通过props传递状态
          { path: 'info/admin', meta: { title: '管理员信息', parent: '快递管理'}, component: () => import('@/views/Admin.vue'),},
          { path: 'info/user', meta: { title: '用户信息', parent: '快递管理'}, component: () => import('@/views/User.vue'),},
          { path: 'updatePassword', meta: { title: '修改密码' }, component: () => import('@/views/UpdatePassword.vue'),},
          { path: 'person', meta: { title: '个人信息' }, component: () => import('@/views/Person.vue'),},
          { path: 'notice', meta: { title: '系统公告' }, component: () => import('@/views/Notice.vue'),},
        ]
    },
    { path: '/login', meta: { title: '登录' }, component: () => import('@/views/Login.vue'),},
    { path: '/mobileHome',  redirect: () => '/mobileHome/check',meta: { title: '移动端首页'}, component: () => import('@/views/MobileHome.vue'),
        children: [
            { path: 'check', meta: { title: '包裹查收' }, component: () => import('@/views/Check.vue'),},
            { path: 'send', meta: { title: '包裹寄退' }, component: () => import('@/views/Send.vue'),}
        ],
    },
    { path: '/mobileAdmin', meta: { title: '入库' }, component: () => import('@/views/MobileAdmin.vue'),},
    { path: '/mobilePerson', meta: { title: '个人中心' }, component: () => import('@/views/MobilePerson.vue'),},
    { path: '/person', meta: { title: '个人信息' }, component: () => import('@/views/Person.vue'),},
    { path: '/notFound', component: () => import('@/views/404.vue'),},
    { path: '/:pathMatch(.*)', redirect: () => '/notFound',}
  ],
})

// 路由全局守卫
router.beforeEach((to) => {
    const user = JSON.parse(localStorage.getItem('code_user') || '{}')
    const needAuthPaths = [
        '/manager',
        '/mobilePerson',
        '/person' // 根据需求添加其他需要登录的路径
    ]

    // 检查路径是否需要登录
    const requiresAuth = needAuthPaths.some(path =>
        to.path.startsWith(path) || to.path === path
    )

    if (requiresAuth && !user?.id) {
        return '/login'
    }
})

export default router
